﻿using System.Collections;
using Gp.Scripts.Data.Skills;
using Gp.Scripts.Equip;
using Helpers;

namespace Gp.Scripts.Core {
    /// <summary>
    /// 由 格挡行为 引起的反应 的参数
    /// </summary>
    public class BlockAttackReaction : ReactionArgs{
        public readonly AttackPreReaction AttackPreReaction;

        
        public BlockAttackReaction(AttackPreReaction attackPreReaction) {
            AttackPreReaction = attackPreReaction;
        }
    }
    
    
    
    /// <summary>
    /// 触发 格挡反应 的处理。在单位进行格挡后，还会触发链式反应
    /// </summary>
    public class BlockAttackReactionEntity : BaseReactionEntity {
        // public BlockAttackReactionEntity(SkillEntityConfig config) : base(config) { }
        private const int ResourceCost = 1;


        public override bool CanReaction(ReactionArgs args) {
            if (args is not AttackPreReaction e) return false;
            // return base.CanReaction(args);
            return e.Target == UnitReference 
                && CheckAction(e) 
                && CheckWeaponType(e)
                && CheckResource(e);
        }

        
        private bool CheckAction(AttackPreReaction e) {
            return e.Stoppable;
        }
        
        
        private bool CheckWeaponType(AttackPreReaction e) {
            // 目标发起近战攻击
            var meleeAttack = MeleeAttack(e);
            
            // 目标单位持有盾牌
            var hasShield = e.Target.ComEquip.HasShield();

            // 目标单位可格挡远程
            var blockRanged = CanBlockRanged(e) || hasShield;

            // var meleeWeapon = e.Target.ComEquip.CurWeapon.Config.GetWeaponType().WeaponBasicType == WeaponBasicType.Melee;
            
            // return (meleeAttack || blockRanged)
                   // 当前单位使用近战武器
                   // && (meleeWeapon || hasShield);
            return false;
        }
        
        private bool CanBlockRanged(AttackPreReaction e) {
            return false;
        }

        private bool MeleeAttack(AttackPreReaction e) {
            // return e.WeaponEntity.Config.GetWeaponType().WeaponBasicType == WeaponBasicType.Melee;
            return false;
        }
        

        // protected override void OnRender(AttackPreReaction e, ReactionRenderInfo info) {
        //     info.Icon = e.Target.Avatar;
        //     info.Title = MeleeAttack(e) ? "格挡" : "格挡远程";
        // }

        public override SkillButtonInfo RenderInfo() {
            return new SkillButtonInfo() {
                Icon = null,
                DescriptionText = "格挡一次攻击",
                NameText = "格挡",
            };
        }


        protected IEnumerator OnTrigger(AttackPreReaction args) {
            UnitReference.ComCost.ReduceResource(CostField.Reaction, ResourceCost);
            UnitReference.GestureTrigger(new BlockAttackGestureTrigger(args.Attacker));
            
            var blockAttackEvent = new BlockAttackReaction(args);
            // 触发格挡事件
            UnitReference.TriggerEvent(new Event_Unit.BlockAttack(args));
            // 尝试触发格挡反应
            yield return BattleLogic.CallReaction(blockAttackEvent);
            
            // 防御减免为0.5
            args.DefencePercent = .5f;
            yield break;
        }


        private bool CheckResource(AttackPreReaction e) {
            // 有防御点（反应） 或“格挡”buff
            return e.Target.ComCost.CheckResource(CostField.Reaction) ;
        }
    }
}